package myjava.util;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author xiaoming
 * @version 1.0
 * @date 2018/11/18 11:31
 * 日历, 时间操作类
 **/
public class CalendarTool {
    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * Calendar的month从0开始, 星期从 SUNDAY 1 开始
     */
    public static void main(String[] args) {
        CalendarTool c = new CalendarTool();
        /*
        1. 取值
         */
        System.out.println("今天期间: " + c.getThisToDay());
        System.out.println("本周期间: " + c.getThisWeekDate());
        System.out.println("本月期间: " + c.getThisMonthDate());

        /*
        2. 实例
         */
        // new
        Calendar calendar = Calendar.getInstance();
        // 初始化
        calendar.setTime(new Date());
        System.out.println("显示年份: " + calendar.get(Calendar.YEAR));
        double x = calendar.get(Calendar.DAY_OF_YEAR);
        double y = calendar.getActualMaximum(Calendar.DAY_OF_YEAR);
        double d =  x/y;
        // 数字格式化
        DecimalFormat df = new DecimalFormat("##.00%");
        System.out.println( "年进度: " + df.format(d));
        //
        Calendar calendarNew = Calendar.getInstance();
        // 设定为5小时以前
        calendarNew.add(Calendar.HOUR, -6);
        // 后者大，显示 -1 ; 前者大，显示 1 ;时间相同，显示 0
        System.out.println("比较时间: " + calendarNew.compareTo(calendar));
        
        /*
        3. 常用方法
         */
        // 设置时间
        calendar.setTime(new Date());
        System.out.println( "获取日历当前Date : "  + calendar.getTime());
        System.out.println( "获取long 时间 : " +  calendar.getTimeInMillis());
        // setTimeInMillis(long millis)b   设置long 时间
        // get(int field)  获取给定时间域的值
        // internalGet(int field)
        // set() 设置 (有多个重载方法
        // clear() 清零时间域, ( isSet返回false)
        // clear(int field) 清零指定时间域
        // isSet(int field) 确定给定的时间域是否设置了数值。
        // complete 填充时间域列表中所有未设置的域。
        // equals(Object obj)   (覆盖：类 Object 中的 equals)
        // before(Object when), after(Object when) 比较
        // add(int field, int amount) 计算
        // roll(int field,boolean up) 时间域滚动
        //  setTimeZone(TimeZone value) 设置时区
        // getTimeZone()
        // setFirstDayOfWeek(int value)
        // getFirstDayOfWeek()
        // getActualMaximum(int field)
        // getActualMinimum(int field)
        // getGreatestMinimum
        //
        // ...


    }

    /**
     * 获取今天的开始和结束
     * @return
     */
    private  String getThisToDay (){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);

        String start =  simpleDateFormat.format(calendar.getTime());

        calendar.set(Calendar.HOUR_OF_DAY,23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);

        String end = simpleDateFormat.format(calendar.getTime());

        return  start + " - " + end;
    }

    /**
     * 获取本周
     */
    private   String getThisWeekDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar =  Calendar.getInstance();
        // 设置一周的第一天
        calendar.setFirstDayOfWeek(Calendar.MONDAY);

        calendar.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE),23, 59, 59);
        String end = sdf.format(calendar.getTime());

        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND,0);
        String start = sdf.format(calendar.getTime());

        return start + " - " + end;
    }

    /**
     * 获取本月期间
     */
    private String getThisMonthDate(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH), calendar.getActualMaximum(Calendar.DAY_OF_MONTH), 23,59, 59);
        String end = simpleDateFormat.format(calendar.getTime());

        calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH), calendar.getActualMinimum(Calendar.DAY_OF_MONTH), 1, 0,0);
        String start = simpleDateFormat.format(calendar.getTime());
        return start + " - " + end;
    }


}
